package com.apestech.framework.file;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

import com.apestech.framework.util.FileUtil;

public class BufferFileWrite {
	
	private String fileName;
	private int index=-1;
	private long fileSize=0;
	private boolean bSize=false;
	private boolean closed=false;
	private FileOutputStream bufferWrite=null;
	
	public BufferFileWrite(String fileName, boolean append) throws Exception{
		this.fileName=fileName;
		FileUtil.newFile(this.fileName);
		if(append){
			bufferWrite = new FileOutputStream(fileName, true);
		} else {
			bufferWrite = new FileOutputStream(fileName);
		}
	}
	
	public String getFileName(){
		return this.fileName;
	}
	
	public void recordSize(){
		this.bSize=true;
	}
	
	public int getIndex(){
		return this.index;
	}
	
	public long getFileSize(){
		return this.fileSize;
	}
	
	public boolean closed(){
		return this.closed;
	}
	
	public void write(String text) throws IOException{
		this.index++;
		text=text+"\n";
		bufferWrite.write(text.getBytes("UTF-8"));
    	if(this.bSize){
    		this.fileSize=this.fileSize+text.getBytes().length;
    	}
	}
	
	public void close() throws IOException{
		this.bufferWrite.flush();
		this.bufferWrite.close();
		this.bufferWrite=null;
		this.closed=true;
	}

}
